/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <dl>
 * <dd>描述: ~ UserController</dd>
 * <dd>创建时间：  11:03 2021/2/23</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/2/23      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@RestController
@RequestMapping("user")
//@CrossOrigin
public class UserController {

    @DubboReference
    private UserService userService;

    /**
     * @Author: guodong
     * @Date: 11:13 2021/2/23
     * @Parms [loginParam]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 用户登录
    */
   /* @PostMapping("login")
    public Result login(@RequestBody LoginParam loginParam){
        //1.根据用户名查询用户
        User user = userService.findUserByName(loginParam.getUsername());
        //2.判断用户是否为null 或者 用户密码是否一致 如果两者有其一不确立那么返回登录失败
        if(null == user || !user.getPassword().equals(loginParam.getPassword())){
            //return new Result(false, "用户名或密码错误");
            return new Result(false, MessageConst.LOGIN_FAIL);
        }
        //3.登录成功
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }*/
    /**
     * @Author: guodong
     * @Date: 9:12 2021/3/8
     * @ClassName: UserController
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 登录成功后的请求地址
    */
   @PostMapping("loginSuccess")
   public Result loginSuccess(){
       return new Result(true, MessageConst.LOGIN_SUCCESS);
   }

    /**
     * @Author: guodong
     * @Date: 9:12 2021/3/8
     * @ClassName: UserController
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 登录失败后的请求地址
     */
   @PostMapping("loginFail")
   public Result loginFail(){
       return new Result(true, MessageConst.LOGIN_FAIL);
   }

   /**
    * @Author: guodong
    * @Date: 10:07 2021/3/8
    * @ClassName: UserController
    * @Parms []
    * @ReturnType: com.itheima.health.entity.Result
    * @Description: 获取用户名称
   */
   @GetMapping("getUserName")
   public Result getUserName(){
       Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
       if(null != obj && obj instanceof User){
           //注意此处的user是security包下面的，不是我们的pojo下面的实体
           User user = (User) obj;
           String username = user.getUsername();
           return new Result(true, MessageConst.GET_USERNAME_SUCCESS, username);
       }
       return new Result(false, MessageConst.GET_USERNAME_FAIL);
   }

   @GetMapping("getMenuByUsername")
    public Result getMenuByUsername() {
       Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
       if (null != obj && obj instanceof User) {
           //注意此处的user是security包下面的，不是我们的pojo下面的实体
           User user = (User) obj;
           String username = user.getUsername();
           //根据会员姓名获取权限
           List<Menu> menus = userService.findMenu(username);
           return new Result(true, MessageConst.ACTION_SUCCESS, menus);
       }
       return new Result(false, MessageConst.ACTION_FAIL);
   }
}
